﻿Imports DTO
Imports System.Data.SqlClient
Public Class CTMONANDAO
    Private ketnoi As New DAO.connect()
    Public Function loadctmonan() As DataTable
        ketnoi.OpenConnection()
        Dim dtnv As New DataTable()
        Dim chuoi As String = "select * from CTMONAN"
        dtnv = ketnoi.getDataTable(chuoi)
        ketnoi.CloseConnection()
        Return dtnv
    End Function
    Private Function Sqlcommand_THEM(ByVal MACHITIETMONAN As Integer, ByVal MAMONAN As Integer, ByVal DONGIAMONAN As Double, ByVal MATIECCUOI As Integer, ByVal GHICHU As String) As Integer
        ketnoi.OpenConnection()
        Dim i As Integer = 0

        Dim sqlcmd As New SqlCommand("Stor_THEM_CTMONAN", ketnoi.getConnection)
        sqlcmd.CommandType = CommandType.StoredProcedure
        'sqlcmd.Parameters.Add("@mode", SqlDbType.Int).Value = mode;
        sqlcmd.Parameters.Add("@MACHITIETMONAN", SqlDbType.Int).Value = MACHITIETMONAN
        sqlcmd.Parameters.Add("@MAMONAN", SqlDbType.Int).Value = MAMONAN
        sqlcmd.Parameters.Add("@DONGIAMONAN", SqlDbType.Float).Value = DONGIAMONAN
        sqlcmd.Parameters.Add("@MATIECCUOI", SqlDbType.Int).Value = MATIECCUOI
        sqlcmd.Parameters.Add("@GHICHU", SqlDbType.NVarChar).Value = GHICHU


        i = sqlcmd.ExecuteNonQuery()
        ketnoi.CloseConnection()
        Return i
    End Function
    Public Function them_CTMONAN(ByVal dto As CTMONANDTO)
        ketnoi.OpenConnection()
        'Dim dto As New LOAISANHDTO()
        If Sqlcommand_THEM(dto.MACHITIETMONAN, dto.MAMONAN, dto.DONGIAMONAN, dto.MATIECCUOI, dto.GHICHU) <> 0 Then
            ketnoi.CloseConnection()
            Return True
        End If
        ketnoi.CloseConnection()
        Return False
    End Function

    Private Function Sqlcommand_capnhat(ByVal MACHITIETMONAN As Integer, ByVal MAMONAN As Integer, ByVal DONGIAMONAN As Double, ByVal MATIECCUOI As Integer, ByVal GHICHU As String) As Integer
        ketnoi.OpenConnection()
        Dim i As Integer = 0

        Dim sqlcmd As New SqlCommand("Stor_CAPNHAT_CTMONAN", ketnoi.getConnection)
        sqlcmd.CommandType = CommandType.StoredProcedure
        'sqlcmd.Parameters.Add("@mode", SqlDbType.Int).Value = mode;
        sqlcmd.Parameters.Add("@MACHITIETMONAN", SqlDbType.Int).Value = MACHITIETMONAN
        sqlcmd.Parameters.Add("@MAMONAN", SqlDbType.Int).Value = MAMONAN
        sqlcmd.Parameters.Add("@DONGIAMONAN", SqlDbType.Float).Value = DONGIAMONAN
        sqlcmd.Parameters.Add("@MATIECCUOI", SqlDbType.Int).Value = MATIECCUOI
        sqlcmd.Parameters.Add("@GHICHU", SqlDbType.NVarChar).Value = GHICHU

        i = sqlcmd.ExecuteNonQuery()
        ketnoi.CloseConnection()
        Return i
    End Function
    Public Function capnhat_ctmonan(ByVal dto As CTMONANDTO) As Boolean
        ketnoi.OpenConnection()
        If Sqlcommand_capnhat(dto.MACHITIETMONAN, dto.MAMONAN, dto.DONGIAMONAN, dto.MATIECCUOI, dto.GHICHU) <> 0 Then
            ketnoi.CloseConnection()
            Return True
        End If
        ketnoi.CloseConnection()
        Return False

    End Function

    Private Function Sqlcommand_XOA(ByVal MACHITIETMONAN As Integer) As Integer
        ketnoi.OpenConnection()
        Dim i As Integer = 0

        Dim sqlcmd As New SqlCommand("Stor_XOA_CTMONAN", ketnoi.getConnection)
        sqlcmd.CommandType = CommandType.StoredProcedure
        sqlcmd.Parameters.Add("@MACHITIETMONAN", SqlDbType.Int).Value = MACHITIETMONAN

        i = sqlcmd.ExecuteNonQuery()
        ketnoi.CloseConnection()
        Return i
    End Function
    Public Function xoa_CTMONAN(ByVal dto As CTMONANDTO) As Boolean
        ketnoi.OpenConnection()
        If Sqlcommand_XOA(dto.MACHITIETMONAN) <> 0 Then
            ketnoi.CloseConnection()
            Return True
        End If
        ketnoi.CloseConnection()
        Return False

    End Function

End Class
